package com.twu.biblioteca;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class BibliotecaApp
{

    protected static final int LOGGEDINMENUCOUNT=6,LOGGEDOUTMENUCOUNT=5;
    static User currentUser=null;

    public static void main(String[] args)
    {
        System.out.println("Welcome To Biblioteca Library Management System !!! ");
        while(displayMenu()!=-1);
    }

    private static String loginBasedMenu()
    {
        if(isLoggedIn())
            return "3. Reserve a Book\n4. Check Membership Details\n5. Log Out\n6. Exit\n\n";
        else
            return "3. Login\n4. Show Membership Details\n5. Exit\n\n";
    }

    private static int displayMenu()
    {
        System.out.print("*********************************************************\n");
        System.out.print("MENU\n1. List All Books\n2. List All Movies\n"+loginBasedMenu());
        System.out.print("Your Choice: ");
        int choice;
        choice=getValidChoice();
        return processChoice(choice);
    }

    private static int getValidChoice()
    {
        int choice;
        try
        {
            choice=Integer.parseInt(getString());
            if(!isValidChoice(choice)) throw new Exception();
        }
        catch(Exception e)
        {
            System.out.print("Select a valid option!!!\n");
            choice=0;
        }
        return choice;
    }

    protected static boolean isValidChoice(int choice)
    {
        return isLoggedIn()?(choice>=1&&choice<=LOGGEDINMENUCOUNT):(choice>=1&&choice<=LOGGEDOUTMENUCOUNT);
    }

    private static int processChoice(int choice)
    {
       if(choice==0)
            return 0;
       if(choice==1)
           Book.displayAll();
       else if(choice==2)
           Movie.displayAll();
       else if(!isLoggedIn())
        {
            if(choice==3)
                login();
            else if(choice==4)
                System.out.println("Please Talk to a Librarian. Thank You");
             else
                return -1;
        }
        else
        {
            if(choice==3)
                Book.reserveMenu();
            else if(choice==4)
                currentUser.showMembershipDetails();
            else if(choice==5)
            {
                logout();
                System.out.println("LOGGED OUT");
            }
            else
                return -1;
        }
        return 0;
    }

    protected static void setCurrentUser(String userName, String password)
    {
        currentUser=User.authenticate(userName,password);
    }
    protected static void login()
    {
        String userName,password;
        System.out.print("Enter User Name: ");
        userName = getString();
        System.out.print("Enter Password: ");
        password = getString();
        setCurrentUser(userName, password);
        if(currentUser!=null)
            System.out.print("LOGGED IN !!!\n");
        else
            System.out.print("LOGIN FAILED !!!\n");
    }

    public static String getString()
    {
        try
        {
            String tmp;
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            tmp=br.readLine();
            //1br.close();
            return tmp;
        }
        catch(Exception e)
        {
            System.out.println("Error Occurred: " + e.toString());
            return "";
        }
    }

    protected static void logout()
    {
        currentUser=null;
    }
    protected static boolean isLoggedIn()
    {
        return currentUser!=null;
    }

}
